package edu.office.applicationRepository;

import edu.office.entities.User;
import edu.office.tools.StaticFields;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
import org.jasypt.util.password.StrongPasswordEncryptor;

import java.io.Serializable;

@Named("parentController")
@ApplicationScoped
public class UserRepository implements Serializable {
    public String exeChangePassword(User user, String oldPassword, String updatedPassword1) {
        //validate whether the old password is correct
        boolean oldCorrect = new StrongPasswordEncryptor().checkPassword(oldPassword, user.getPassword());
        //If true
        if (oldCorrect) {
            user.setPassword(StaticFields.encrypt(updatedPassword1));
            return  "Succeed";
        } else {
            return  "Failed";
        }
    }

    public void reset2DefaultPassword(User user) {
        user.setPassword(new StrongPasswordEncryptor().encryptPassword(StaticFields.DefaultNumber));
    }
}
